package com.catsic.mod.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.catsic.base.BaseServiceImpl;
import com.catsic.base.exception.CatsicException;
import com.catsic.core.organ.service.OrganService;
import com.catsic.core.user.service.UserService;
import com.catsic.mod.condition.AnnouncesCondition;
import com.catsic.mod.convert.Converts;
import com.catsic.mod.dao.AnnouncesDao;
import com.catsic.mod.entity.Announces;
import com.catsic.mod.exception.CatsicExceptionCode;
import com.catsic.mod.form.AnnouncesForm;
import com.catsic.mod.service.AnnouncesService;
import com.catsic.mod.specification.AnnouncesSpec;
import com.catsic.pub.drawer.service.DrawerService;

@Service("announcesService")
public class AnnouncesServiceImpl extends BaseServiceImpl<Announces, String> implements AnnouncesService{


	@Autowired
	private AnnouncesDao announcesDao;
	
	@Autowired
	private OrganService organService;
	@Autowired
	private DrawerService drawerService;
	@Autowired
	private UserService userService;
/*	@Override
	protected BaseDao<Announces, String> getEntityDao() {
		return announcesDao;
	}*/
	
	/**
	 * data = 
	 * data.organIds=organIds.join(",")
	 */
	@Override
	public AnnouncesForm add(AnnouncesForm form,String[] organIds) {
		if(null==drawerService.findById(form.getType())){
			return null;
		}
		if(null==userService.findById(form.getCreateId())){
			return null;
		}
		String organId_="";
		if(organIds.length > 0){
			for(String organId : organIds){
				organId_+=organId+",";
			}
			int index=organId_.lastIndexOf(",");
			if(index == organId_.length()-1){
				organId_=organId_.substring(0,organId_.length()-1);
			}
		}
		Announces announces = new Announces();
		announces.setUser(userService.findById(form.getCreateId()));
		announces.setDrawer(drawerService.findById(form.getType()));
		announces.setFinishDate(form.getFinishDate());
		announces.setContent(form.getContent());
		announces.setTitle(form.getTitle());
		announces.setOrganId(organId_);
		super.add(announces);
		return Converts.convert(announces);
	}

	@Override
	public Announces findById(String id) {
		Announces announces = getById(id);
		if(null==announces){
			throw new CatsicException(CatsicExceptionCode.ANNOUNCES_NOT_FOUND);
		}
		return announces;
	}

	@Override
	public AnnouncesForm findAnnouncesById(String id) {
		Announces announces = findById(id);
		return Converts.convert(announces);
	}

	@Override
	public Page<AnnouncesForm> findAll(AnnouncesCondition condition,
			Pageable pageable) {
		AnnouncesSpec spec = new AnnouncesSpec(condition);
		Page<Announces> page = announcesDao.findAll(spec,pageable);
		AnnouncesForm form = new AnnouncesForm();
		List<AnnouncesForm> listForm = new ArrayList<AnnouncesForm>();
		for(Announces announces : page.getContent()){
			form = Converts.convert(announces);
			listForm.add(form);
		}
		Page<AnnouncesForm> page_ = new PageImpl<AnnouncesForm>(listForm,pageable,page.getTotalElements());
		return page_;
	}



}
